/**
 * Created by zwsvane on 2017/9/13.
 */

$(function(){

    //查询接口调试

    function hgxsqsList(provinceId,cityId,siteId,beginTime,endTime,keyWords,pageNum){

        $.ajax({
            'type':'post',
            'dataType':'json',
            'url':WEBURL+'/coefficientTrend/list',
            'data':{
                'provinceId':provinceId,
                'cityId':cityId,
                'siteId':siteId,
                'beginTime':beginTime,
                'endTime':endTime,
                'keyWords':keyWords,
                'pageNo':pageNum,
                'pageSize':10
            },
            success:function(r){
                if(r.code==0000){
                    var gpxsqsList ='';
                    $("#gpxsqsList").html('');
                    console.log(r.data.dataList.length);
                    if(r.data.dataList.length>0){
                        $.each(r.data.dataList,function(index,item){
                            gpxsqsList = gpxsqsList + '<tr class="text-c">'+
                              //  '<td><input type="checkbox" value="'+item.id+'" name="" hidden="hidden"></td>'+
                            '<td>'+item.provinceName+'<input type="checkbox" value="'+item.id+'" name="" hidden="hidden"></td>'+
                            '<td>'+item.cityName+'</td>'+
                            '<td>'+item.siteName+'</td>'+
                            '<td>'+item.workerNum+'</td>'+
                            '<td>'+item.orderEfficientNum+'</td>'+
                            '<td>'+item.coefficient+'</td>'+
                            '<td>'+item.siteDate+'</td>'+
                            '<td><span onClick="system_category_add(\''+item.siteName+'公平系数趋势\',\'gpxsqs-repair.html?sideId='+item.relationId+'&xishu='+item.coefficient+'\',1100,600)" title="'+item.siteName+'" class="btn btn-secondary radius">查看</span></td>'+
                            '</tr>';


                        });

                    }
                    else{
                        gpxsqsList = '<tr class="text-c"><td colspan="9">暂无数据</td></tr>';
                    }
                    $("#kkpager").html('');
                    //分页
                    var totaldata = r.data.totalRecord;
                    var totalpages = Math.floor((totaldata-1)/10)+1;

                    var totalPage = totaldata;
                    var pages =  totalpages;
                    //console.log(pages);
                    var totalRecords = totaldata;
                    var pageNo = localStorage.nn;
                    console.log(pageNo);
                    if(!pageNo) {
                        pageNo = 0;
                    }
                    //有些参数是可选的，比如lang，若不传有默认值
                    kkpager.generPageHtml({
                        pno: pageNo,
                        //总页码
                        total: totalpages,
                        //总数据条数
                        totalRecords: totaldata,
                        mode: 'click', //默认值是link，可选link或者click
                        click: function(n) {
                            if($("#provinceList").find("option:selected").attr("data-orgCode")==''||$("#provinceList").find("option:selected").attr("data-orgCode")==' '||$("#provinceList").find("option:selected").attr("data-orgCode")==undefined){
                                $("#provinceList").find("option:selected").attr("data-orgCode",'-1');
                            }

                            var zdyl_provinceList =$("#provinceList").find("option:selected").attr("data-orgCode");
                            var zdyl_cityCompany =$("#cityList").find("option:selected").attr("data-orgCode");
                            var zdyl_siteId =$("#siteList").find("option:selected").attr("data-orgCode");
                            var zdyl_beginTime =$("#startTime").val();
                            var zdyl_endTime =$("#endtime").val();
                            var zdyl_keyWords =$("#zdyl-keyWords").val();


                            // do something
                            //手动选中按钮
                            this.selectPage(n);
                            localStorage.nn = n;
                            hgxsqsList(zdyl_provinceList,zdyl_cityCompany,zdyl_siteId,zdyl_beginTime,zdyl_endTime,zdyl_keyWords,(localStorage.nn-1));
                            return false;
                        }

                    },true);

                    $(gpxsqsList).appendTo($("#gpxsqsList"));

                }
            },
            error:function(e){
                console.log(e.msg)
            }
        })

    }

    //条件查询
    $(".gpxsqs-search").on("click",function(){
        localStorage.nn = 0;

        var zdyl_provinceList =$("#provinceList").find("option:selected").attr("data-orgCode");
        var zdyl_cityCompany =$("#cityList").find("option:selected").attr("data-orgCode");
        var zdyl_siteId =$("#siteList").find("option:selected").attr("data-orgCode");
        var zdyl_beginTime =$("#startTime").val();
        var zdyl_endTime =$("#endtime").val();
        var zdyl_keyWords =$("#zdyl-keyWords").val();

        console.log(zdyl_provinceList);

        if(zdyl_beginTime==''||zdyl_beginTime==undefined||zdyl_beginTime==null){

                layer.msg('请输入开始时间！');
                return;
             }

            if(zdyl_endTime==''||zdyl_endTime==undefined||zdyl_endTime==null){

                layer.msg('请输入结束时间！');
                return;
            }
            if(zdyl_endTime<zdyl_beginTime){

                layer.msg('输入的结束时间不能小于开始时间！');
                return;
            }
        if(zdyl_provinceList==''||zdyl_provinceList==undefined||zdyl_provinceList==null||zdyl_provinceList==' '){
            zdyl_provinceList ='-1';
            zdyl_cityCompany ='-1';
                zdyl_siteId = '-1';
        };

        if(zdyl_cityCompany==''||zdyl_cityCompany==' '||zdyl_cityCompany==undefined||zdyl_cityCompany==null){
            zdyl_cityCompany ='-1';
            $("#cityList").find("option:selected").attr("data-orgCode",'-1');
        };
        if(zdyl_siteId==''||zdyl_siteId==' '||zdyl_siteId==undefined||zdyl_siteId==null){
            zdyl_siteId ='-1';
            $("#siteList").find("option:selected").attr("data-orgCode",'-1');
        };
        hgxsqsList(zdyl_provinceList,zdyl_cityCompany,zdyl_siteId,zdyl_beginTime,zdyl_endTime,zdyl_keyWords,0);

    })

    //关键词查询判断 省份不可选
    $("#zdyl-keyWords").blur(function(){

        if($(this).val()==''){
            $("#provinceList").prop('disabled',false);
            $("#cityList").prop('disabled',false);
            $("#siteList").prop('disabled',false);
        }
        else{
            $("#provinceList").prop('disabled','true');
            $("#cityList").prop('disabled','true');
            $("#siteList").prop('disabled','true');
        }

    });
    //省份查询 关键字不可输入
    $("#provinceList").change(function(){
        $("#zdyl-keyWords").prop('disabled','true');
    });

    //查询条件重置
    $(".gpxsqs-reset").on("click",function(){
        $("#provinceList option").eq(0).prop("selected",'selected');
        $("#cityList option").eq(0).prop("selected",'selected');
        $("#siteList option").eq(0).prop("selected",'selected');
        $("#zdyl-keyWords").prop("disabled",false);
        $("#zdyl-keyWords").val('');
        $("#provinceList").prop('disabled',false);
        $("#cityList").prop('disabled',false);
        $("#siteList").prop('disabled',false);
        $("#startTime").val('');
        $("#endtime").val('');

    });

    //数据导出
    $(".pgsxqs-export-btn").on("click",function(){
        if($("#startTime").val()==''){
            layer.msg('请输入查询时间！');
            return;
        }if($("#endtime").val()==''){
            layer.msg('请输入查询时间！');
            return;
        }
        if($("#provinceList option:selected").attr('data-orgcode')==''){
            $("#provinceList option:selected").attr('data-orgcode','-1');
             $("#cityList  option:selected").attr('data-orgcode','-1');
            $("#siteList option:selected").attr('data-orgcode','-1')
        }

        var timeLoad =   window.setTimeout(function(){
            $.Huimodalalert('数据下载中,请稍后。。。。',2000);
        },200);
        var oFrm = document.getElementById('iframe');
        oFrm.onload = oFrm.onreadystatechange = function() {
            if (this.readyState && this.readyState != 'complete') return;
            else {

                window.clearTimeout(timeLoad);
                alert('未查询到数据！');
            }
        }
        var url =WEBURL +'/coefficientTrend/export?provinceId='+$("#provinceList option:selected").attr('data-orgcode')+'&cityId='+$("#cityList  option:selected").attr('data-orgcode')+'&siteId='+$("#siteList option:selected").attr('data-orgcode')+'&beginTime='+$("#startTime").val()+'&endTime='+$("#endtime").val()+'&keyWords='+ $("#zdyl-keyWords").val();
        $("#iframe").attr("src",url);

    })


})

